package com.pingan.haofang.searchcloud.api.query.parser.dbobject;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * ClassName: ConditionGroup <br>
 * Function: 条件组，代表连续的and条件集合<br>
 *
 * @author luyi
 * @version 1.0.0
 * @since 1.0.0
 * @date 20150926
 */
public class ConditionGroup {

    private ConditionGroup parent;
    private List<ConditionGroup> childList;
    private List<Condition> conditionList;
    private Set<Integer> orIndexSet;

    /**
     * 针对保留条件d的特殊处理（蛋疼）
     */
    private Double distance;

    public boolean isEmpty() {
        return (childList == null || childList.isEmpty())
                && (conditionList == null || conditionList.isEmpty());
    }

    public boolean isChild() {
        return childList == null || childList.isEmpty();
    }

    public void addChild(ConditionGroup group) {
        if (childList == null) {
            childList = new ArrayList<ConditionGroup>();
        }
        group.setParent(this);
        childList.add(group);
    }

    public void addCondition(Condition condition) {
        if (conditionList == null) {
            conditionList = new ArrayList<Condition>();
        }
        conditionList.add(condition);
    }

    public ConditionGroup getParent() {
        return parent;
    }

    public void setParent(ConditionGroup parent) {
        this.parent = parent;
    }

    public List<ConditionGroup> getChildList() {
        return childList;
    }

    public void setChildList(List<ConditionGroup> childList) {
        this.childList = childList;
    }

    public List<Condition> getConditionList() {
        return conditionList;
    }

    public void setConditionList(List<Condition> conditionList) {
        this.conditionList = conditionList;
    }

    public Set<Integer> getOrIndexSet() {
        return orIndexSet == null ? new HashSet<>(0) : orIndexSet;
    }

    public void setOrIndexSet(Set<Integer> orIndexSet) {
        this.orIndexSet = orIndexSet;
    }

    public Double getDistance() {
        return distance;
    }

    public void setDistance(Double distance) {
        this.distance = distance;
    }

    @Override
    public String toString() {
        if (childList != null) {
            return String.valueOf(childList);
        } else {
            return String.valueOf(conditionList);
        }
    }

}
